package sg.edu.nus.iss.vmcs.system;

/*
 * Copyright 2011 ISS.
 * The contents contained in this document may not be reproduced in any
 * form or by any means, without the written permission of ISS, other
 * than for the purpose for which it has been supplied.
 *
 */

import java.io.*;

import sg.edu.nus.iss.vmcs.maintenance.*;
import sg.edu.nus.iss.vmcs.machinery.*;
import sg.edu.nus.iss.vmcs.customer.*;
import sg.edu.nus.iss.vmcs.store.*;
import sg.edu.nus.iss.vmcs.util.*;

/**
 * This class represents the main controller object of the VMCS application. It
 * coordinates the main operations of the VMCS. It instantiates most of the
 * controllers {@link SimulationController}, {@link MachineryController},
 * {@link MaintenanceController}, {@link TransactionController},
 * {@link StoreController}.
 * 
 * @version 1.0 09/12/2011
 * @author Team 7S - SE19
 */

public class MainController {

	private SimulationController simulatorCtrl;
	private MachineryController machineryCtrl;
	private MaintenanceController maintenanceCtrl;
	private TransactionController transactionCtrl;
	private StoreController storeCtrl;

	private String propertyFile;

	/**
	 * This constructor creates an new instance of Main Controller.
	 * 
	 * @param propertyFile
	 *            property file
	 */
	public MainController(String propertyFile) {
		this.propertyFile = propertyFile;
	}

	/**
	 * This operation will initiate the creation of all the control objects
	 * necessary for simulation of the VMCS and initiate the display of the
	 * Simulator Control Panel (the main menu).
	 * 
	 * @throws VMCSException
	 *             Initiation Exception
	 * @see SimulationController
	 */
	public void start() throws VMCSException {
		try {
			initialize();
			simulatorCtrl.displaySimulatorControlPanel();
			simulatorCtrl.setSimulationActive(false);
		} catch (VMCSException e) {
			throw new VMCSException(e);
		}
	}

	/**
	 * This operation creates all the control objects.
	 * 
	 * @throws VMCSException
	 *             Initialization Exception
	 * @see CashPropertyLoader
	 * @see DrinkPropertyLoader
	 * @see StoreController
	 * @see SimulationController
	 * @see MachineryController
	 * @see MaintenanceController
	 * @see TransactionController
	 */
	public void initialize() throws VMCSException {
		try {
			Environment.initialize(propertyFile);
			IStoreLoader cashPropertyLoader = new FilePropertyLoader(Environment.getCashPropFile());
			IStoreLoader drinkPropertyLoader = new FilePropertyLoader(Environment.getDrinkPropFile());
			
			cashPropertyLoader.initialize();
			drinkPropertyLoader.initialize();
			
			ItemLoader cashLoader =
				new CashItemLoader(cashPropertyLoader);
			ItemLoader drinksLoader =
				new DrinkItemLoader(drinkPropertyLoader);
			
			//Init Singleton Controllers
            storeCtrl=StoreController.getInstance();	
            storeCtrl.initialize(cashLoader, drinksLoader);

			simulatorCtrl = new SimulationController(this);
			machineryCtrl = new MachineryController(this);
			machineryCtrl.initialize();
			maintenanceCtrl = new MaintenanceController(this);
			transactionCtrl = new TransactionController(this);
		} catch (IOException e) {
			throw new VMCSException(
				"MainController.initialize",
				e.getMessage());
		}
	}

	/**
	 * This operation returns reference to SimulationController object.
	 * 
	 * @return Simulation Controller
	 */
	public SimulationController getSimulationController() {
		return simulatorCtrl;
	}

	/**
	 * This operation returns the reference to SimulatorControlPanel object.
	 * 
	 * @return Simulator Control panel
	 */

	public SimulatorControlPanel getSimulatorControlPanel() {
		return simulatorCtrl.getSimulatorControlPanel();
	}

	/**
	 * This operation returns reference to StoreController object.
	 * 
	 * @return Store Controller
	 */

	public StoreController getStoreController() {
		return storeCtrl;
	}

	/**
	 * This operation returns reference to MachineryController object.
	 * 
	 * @return Machinery Controller
	 */

	public MachineryController getMachineryController() {
		return machineryCtrl;
	}

	/**
	 * This operation returns reference to MaintenanceController object.
	 * 
	 * @return Maintenance Controller
	 */
	public MaintenanceController getMaintenanceController() {
		return maintenanceCtrl;
	}

	/**
	 * This operation returns reference to TransactionController object.
	 * 
	 * @return Transaction Controller
	 */

	public TransactionController getTransactionController() {
		return transactionCtrl;
	}

	/**
	 * This operation destroys all the object instances created for operating
	 * VMCS. It will instruct the Simulation Controller to close down (by
	 * closing down all the VMCS Panels) and the Transaction Controller to close
	 * down. It will also close down other control objects and the entity
	 * objects created for simulating the VMCS.
	 */

	public void closeDown() {
		try {
			storeCtrl.closeDown();
		} catch (Exception e) {
			System.out.println("Error closing down the stores: " + e);
		}
		machineryCtrl.closeDown();
		maintenanceCtrl.closeDown();
		simulatorCtrl.closeDown();
	}
}
